//
//  SetPasswordViewController.m
//  CommunityNannyO2O
//
//  Created by 90 on 15/7/21.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "SetPasswordViewController.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "LoginViewController.h"
#import "UserDataSingleton.h"

@interface SetPasswordViewController()<QXNetWorkToolDelegate>
{

}

@property (weak, nonatomic) IBOutlet CustomTextField  *password;

@property (weak, nonatomic) IBOutlet CustomTextField  *comfrimPassword;

@property (weak, nonatomic) IBOutlet UIButton *confirm;

@end


@implementation SetPasswordViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self configNavigationItem];
    
    
    //叉号，一次性删除输入框中的内容
    [self.password setClearButtonMode:UITextFieldViewModeWhileEditing];
    [self.comfrimPassword setClearButtonMode:UITextFieldViewModeWhileEditing];
    
    self.password.secureTextEntry = YES;
    self.comfrimPassword.secureTextEntry = YES;
  
    //编辑时清空
    self.password.clearsOnBeginEditing = YES;
    self.comfrimPassword.clearsOnBeginEditing = YES;

    //placeholder字体、颜色
    [self.password setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:15]
                      Color:UIColorRGBA(169, 169, 169, 1)];
    
    [self.comfrimPassword setFont:[UIFont fontWithName:@"Hiragino Sans GB" size:13] Color:UIColorRGBA(169, 169, 169, 1)];

    
    self.confirm.layer.cornerRadius = 5;
    self.confirm.layer.masksToBounds = YES;
    [self.confirm.layer setBorderWidth:1.0]; //边框宽度
    [self.confirm.layer setBorderColor:UIColorRGBA(19, 208, 202, 1).CGColor];//边框颜色

    
    //子view
    UIView *containView1 = [self.view viewWithTag: 1];
    
    UITapGestureRecognizer *tapGestureRecognizer1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView1 addGestureRecognizer:tapGestureRecognizer1];
    
    //子view
    UIView *containView2 = [self.view viewWithTag: 2];
    
    UITapGestureRecognizer *tapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
    [containView2 addGestureRecognizer:tapGestureRecognizer2];
}

- (BOOL)isCorrectPassword
{
    return [self.password.text isEqualToString:self.comfrimPassword.text];
}

- (IBAction)confirm:(id)sender
{
    if (self.password.text.length == 0
        || self.comfrimPassword.text.length == 0)
    {
        [MBProgressHUD showError:@"请输入密码"];
        return;
    }
    
    if ([self isCorrectPassword] == NO)
    {
        [MBProgressHUD showError:@"请确保两次密码输入一致"];
        return;
    }
    
    [MBProgressHUD showMessage:@"正在加载"];
    
    QXNetWorkTool *QXnetWorkTool = [[QXNetWorkTool alloc]init];
    
    QXnetWorkTool.QXNetWorkDelegate = self;
    
    [QXnetWorkTool GetDataFromServerWithPostMethodandInterfaceName:ModifyInterface andParameter:@[@{
                            @"phone":self.phone,
                            @"pwd":self.password.text}] tag:1];
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message];
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    [MBProgressHUD hideHUD];
    
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:mes];
            
            UserDataSingleton * userdata = [UserDataSingleton shareInstance];
            if(![userdata isLogin])
            {
                [userdata pleaseLogin:self];
            }
            break;
    }
}

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    switch (tag)
    {
        case 1:
            
            if (Response == nil)
            {
                
                return;
            }
            
            
            for (NSDictionary *dic in Response)
            {

            }
            
            break;
    }
}

-(void)tap
{
    [self.password resignFirstResponder];
    [self.comfrimPassword resignFirstResponder];
}

-(void)configNavigationItem
{
    self.navigationItem.title = @"设置密码";
    [self.navigationController.navigationBar setTranslucent:NO];
    
    [self.navigationController.navigationBar
     setBackgroundImage:[UIImage imageNamed:@"bg_login_statue_bar"]
     forBarMetrics:UIBarMetricsDefault];
    
    //status bar的字体为白色
    //导航栏的背景色是黑色
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],NSFontAttributeName,nil]];
    
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
}

-(void)useMethodToFindBlackLineAndHind
{
    UIImageView* blackLineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    
    blackLineImageView.hidden = YES;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self configNavigationItem];

    [self useMethodToFindBlackLineAndHind];
}

-(void)viewWillDisappear:(BOOL)animated
{

}


@end
